import 'package:flutter/material.dart';
import 'package:flutter_record_button/record_bottom_sheet.dart';
import 'record_button.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Record Button Demo',
      home: Scaffold(
        appBar: AppBar(title: const Text('Record Button Demo')),
        // body: const Center(child: RecordButtonDemo()),
        body: const Center(child: HomeScreen()),
      ),
    );
  }
}


class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  // 申请权限的方法
  Future<bool> _requestPermission() async {
    var status = await Permission.microphone.request();
    return status.isGranted;
  }

  // 录音开始回调
  void _onRecordStart() {
    print("录音开始");
  }

  // 录音结束回调
  void _onRecordEnd(Duration duration, String filePath) {
    print("录音结束，时长: ${duration.inMilliseconds}ms, 文件: $filePath");
    // 你可以在这里处理录音文件
  }

  // 处理权限被永久拒绝的回调
  void _onPermissionPermanentlyDenied(BuildContext context) {
    print("录音权限被永久拒绝");
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("权限被拒"),
          content: const Text("请在设置中授予录音权限。"),
          actions: <Widget>[
            TextButton(
              child: const Text("取消"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text("去设置"),
              onPressed: () {
                Navigator.of(context).pop();
                openAppSettings(); // 打开应用设置页面
              },
            ),
          ],
        );
      },
    );
  }

  // 弹出底部控件的方法
  void _showRecordBottomSheet(BuildContext context) {
    RecordBottomSheet.show(
      context: context,
      text: "这里是居中动态文本内容", // 传入动态文本
      topOffset: 100.0, // 可以设置距离顶部的高度，这里示例设置为 100
      onDismiss: () {
        print("底部弹窗已关闭");
      },
      // 传递 RecordButton 的回调
      onNeedPermission: _requestPermission,
      onRecordStart: _onRecordStart,
      onRecordEnd: _onRecordEnd,
      onPermissionPermanentlyDenied: () => _onPermissionPermanentlyDenied(context),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("底部弹窗示例"),
      ),
      body: Center(

        child: ElevatedButton(
          onPressed: () => _showRecordBottomSheet(context), // 点击按钮弹出底部控件
          child: const Text("显示录音底部弹窗"),
        ),
      ),
    );
  }
}

class RecordButtonDemo extends StatelessWidget {
  const RecordButtonDemo({super.key});

  // 申请权限的方法
  Future<bool> _requestPermission() async {
    // 使用 request() 方法会弹出系统权限申请框
    var status = await Permission.microphone.request();
    // 返回最终是否授予了权限的状态
    return status.isGranted;
  }

  // 录音开始回调
  void _onStart() {
    print("开始录音");
  }

  // 录音结束回调
  void _onEnd(Duration duration, String filePath) {
    print("录音结束，时长: ${duration.inMilliseconds}ms, 文件: $filePath");
    // 你可以在这里上传文件或播放录音
  }

  // 处理权限被永久拒绝的回调
  void _onPermissionPermanentlyDenied(BuildContext context) {
    print("录音权限被永久拒绝");
    // 显示一个弹窗或SnackBar提示用户并引导去设置页面
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("权限被拒"),
          content: Text("请在设置中授予录音权限。"),
          actions: <Widget>[
            TextButton(
              child: Text("取消"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: Text("去设置"),
              onPressed: () {
                Navigator.of(context).pop();
                openAppSettings(); // 打开应用设置页面
              },
            ),
          ],
        );
      },
    );
  }


  @override
  Widget build(BuildContext context) {
    return RecordButton(
      onNeedPermission: _requestPermission, // 传递申请权限的方法
      onRecordStart: _onStart,
      onRecordEnd: _onEnd,
      onPermissionPermanentlyDenied: () => _onPermissionPermanentlyDenied(context), // 传递处理永久拒绝的回调
    );
  }
}